package com.minhua.jijiehao2.mainframe.account.accountClassFragment.impl

import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentPagerAdapter
import com.minhua.jijiehao2.R
import com.minhua.jijiehao2.base.BaseFragment
import com.minhua.jijiehao2.mainframe.account.accountClassFragment.imp.IAccountClassView
import com.minhua.jijiehao2.mainframe.account.accountlistfragment.impl.AccountListFragment
import kotlinx.android.synthetic.main.fragment_account_class.*

class AccountClassFragment : BaseFragment<IAccountClassView, AccountClassPresenter>(), IAccountClassView {

    override fun getLayoutId(): Int = R.layout.fragment_account_class

    override fun getPresenter(): AccountClassPresenter = AccountClassPresenter()

    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        if (!hidden) {
            fragments
                    .filterIsInstance<AccountListFragment>()
                    .forEach { it.initData() }
        }
    }

    val title = arrayListOf("未完成", "已完成")
    val fragments = arrayListOf<Fragment>(AccountListFragment.newInstance(0), AccountListFragment.newInstance(1))

    override fun initView() {
        vpContent.adapter = object : FragmentPagerAdapter(activity.supportFragmentManager) {
            override fun getItem(position: Int): Fragment = fragments[position]

            override fun getCount(): Int = fragments.size

            override fun getPageTitle(position: Int): CharSequence = title[position]
        }

        tlTab.setSelectedTabIndicatorColor(resources.getColor(R.color.mainRed))
        tlTab.setupWithViewPager(vpContent)
    }

    companion object {

        fun newInstance(): AccountClassFragment {
            val fragment = AccountClassFragment()
            val args = Bundle()
            fragment.arguments = args
            return fragment
        }
    }
}
